﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.PRM.AppCode.Lib;

namespace mojoPortal.Web.RS.PRM.Controls
{
    public partial class PropertyFeatureControl : RSBaseUserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }
        public bool LoadPropertyFeatures(int companyid, int propertyid)
        {
            hfPropertyID.Value = propertyid.ToString();

            bool bHasValue = true;
            RSPRMDataLookupLoader.LoadTypeDDL(companyid, "FEATURE_CAT", ddlFeatureType);

            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            String sqlSELECT = "SELECT f.FeatureID, (SELECT t.TypeName FROM rs_tblType t WHERE t.TypeID=f.FeatureTypeID) AS FeatureType, f.PropertyID, f.FeatureValue FROM rs_tblPropertyFeature f WHERE f.PropertyID=@PropertyID AND f.CompanyID=@CompanyID";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@propertyid";
            param.Value = propertyid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            gvFeatures.DataSource = ds;
            gvFeatures.DataBind();
            myConnection.Close();

            if (gvFeatures.Rows.Count == 0)
            {
                bHasValue = false;
                return bHasValue;
            }
            return bHasValue;
        }

        protected void btnAddFeature_Click(object sender, EventArgs e)
        {
            int FeatureTypeID = int.Parse(ddlFeatureType.SelectedValue);
            string FeatureValue = txtFeature.Text;

            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int companyid = userIdentity.RSCompanyId;
                int propertyid = hfPropertyID.Value == "" ? 0 : int.Parse(hfPropertyID.Value);

                if (companyid > 0 && propertyid > 0 && FeatureValue.Length > 0)
                {
                    string SQL = "INSERT INTO dbo.rs_tblPropertyFeature " +
                    "(PropertyID,FeatureTypeID,FeatureValue,Audit_CreateDt,Audit_CreateBy,CompanyID) " +
                    "VALUES (@PropertyID,@FeatureTypeID,@FeatureValue,GETDATE(),@CurrentUser,@CompanyID)";
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyID", SqlDbType.Int, 0, ParameterDirection.Input, propertyid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@FeatureTypeID", SqlDbType.Int, 0, ParameterDirection.Input, FeatureTypeID);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@FeatureValue", SqlDbType.NVarChar, 250, ParameterDirection.Input, FeatureValue);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 30, ParameterDirection.Input, userIdentity.RSCurrentUser);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, SQL);
                    SQLHelper.ExecuteScalarCmd(sqlCmd);

                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                    //ucModalActionMessageControl.ShowModal("Feature Added Successfully!", RedirectToCurrentURL("2"), true);
                    LoadPropertyFeatures(companyid, propertyid);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
        protected void gvFeatures_Remove(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int featureid = Convert.ToInt32(e.CommandArgument.ToString());
                int propertyid = int.Parse(hfPropertyID.Value);

                if (companyid > 0 && featureid > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@FeatureID", SqlDbType.Int, 0, ParameterDirection.Input, featureid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyID", SqlDbType.Int, 0, ParameterDirection.Input, propertyid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblPropertyFeature WHERE PropertyID=@PropertyID AND FeatureID=@FeatureID AND CompanyId=@companyid");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);

                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                    //ucModalActionMessageControl.ShowModal("Feature Deleted Successfully!", RedirectToCurrentURL("2"), true);
                    LoadPropertyFeatures(companyid, propertyid);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        private string RedirectToCurrentURL(string activetab)
        {
            var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
            nameValues.Set("selindex", activetab);
            string url = Request.Url.AbsolutePath;
            string updatedQueryString = "?" + nameValues.ToString();
            url = url + updatedQueryString;
            return url;
        }
    }
}